package com.gidgabs.modules.api.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.gidgabs.common.annotation.LoginUserApi;
import com.gidgabs.common.utils.PageUtils;
import com.gidgabs.common.utils.Query;
import com.gidgabs.common.utils.R;
import com.gidgabs.common.utils.ReturnJson;
import com.gidgabs.common.validator.Assert;
import com.gidgabs.modules.api.service.TbProductRewardService;
import com.gidgabs.modules.api.service.TbUserService;
import com.gidgabs.modules.entity.TbProductRewardEntity;
import com.gidgabs.modules.entity.TbUser;
import com.gidgabs.utils.SessionDataUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 产品关系表-打赏表
 *
 * @author lanyonglin
 * @email www.baidu.com
 * @date 2018-06-29 17:16:11
 */
@Api("产品打赏模块")
@RestController
@RequestMapping("/api/productreward")
public class TbProductRewardController {

    @Autowired
    private TbProductRewardService productRewardService;

    @Autowired
    private TbUserService userService;

    /**
     * 添加打赏
     *
     * @param productId
     * @param money
     * @return
     */
    @ApiOperation(value = "添加打赏")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productId", value = "产品id", dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "money", value = "打赏金额", dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "token", value = "token", dataType = "string", paramType = "header")
    })
    @PostMapping("/save")
    public ReturnJson<TbProductRewardEntity> save(Integer productId, Integer money) {
        //获取用户信息
        TbUser tbUser = new TbUser();
        tbUser.setUserId(SessionDataUtil.getCurrentUserId());
        TbUser user = userService.queryObject(tbUser);
        if (user == null) {
            return ReturnJson.fail("登录用户不存在！");
        }
        Assert.isNull(money, "参数不能为空");
        TbProductRewardEntity tbProductRewardEntity = new TbProductRewardEntity();
        tbProductRewardEntity.setUserIdMaster(user.getUserId());
        tbProductRewardEntity.setRewardIdSlave(productId);
        tbProductRewardEntity.setMoney(money);
        tbProductRewardEntity.setCreateTime(new Date());
        productRewardService.save(tbProductRewardEntity);
        return ReturnJson.success("添加成功");
    }

}
